/**
 * Created by kevin on 2017/07/12.
 *
 * 通用Scroller
 * params
 * obj:显示对象
 * objMask:{"w":111,"h":"222"}
 * fit:{"type":"exact_fit"}//等比撑满
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function Scroller(obj,objmask,fit) {

        this.Container_constructor();
        this.obj = obj;
        this.objMask = objmask;
        this.fit = fit;
        this.init();
        //console.log(this.father.targetWidth);

    }



    var p = createjs.extend(Scroller, createjs.Container);

    p.className = "Scroller";
    //被滚动显示对象
    p.obj;
    //遮罩
    p.objMask;
    p.fit;
    p.s_mask;

    p.bg;

    p.isPressed = false;
    p.offset_y = 0;
    p.start_y = 0;
    p.end_y = 0;

    // public methods:
    p.init = function () {
        this.drawBg();
        this.showObj();
        this.drawMask();

        this.on("mousedown",this.onMouseDown,this);
        this.on("pressup",this.onPressup,this);
        this.on("pressmove",this.onMouseMove,this);

        createjs.Ticker.on("tick", this.tick,this);
        Scroller.SCROLLER = this;
    }

    p.tick = function(evt){
        //var _this =
        //console.log(evt)

    }

    p.onMouseMove = function(evt){
        //console.log("onMouseMove")
        this.end_y = evt.stageY
        this.offset_y = this.end_y - this.start_y;
        var toY = this.real_y + this.offset_y;
        //console.log(toY)
        if(toY > this.objMask.h*.2){
            toY = this.objMask.h*.2;
        }
        this.obj.y =  toY;
    }

    p.onPressup = function(evt){
        this.isPressed = false;
        //console.log("onPressup")
        this.real_y = this.obj.y;
        var min_y = this.obj.getBounds().height*this.obj.scaleX-this.objMask.h;
        if(!this.isPressed && this.obj){
            if(this.obj.y > 10){

                createjs.Tween.get(this.obj).to({y:0}, 1000,createjs.Ease.backOut);

                //this.obj.y = 0;
            }else if(this.obj.y > -min_y && this.obj.y <= 10){
                createjs.Tween.get(this.obj).to({y:-min_y}, 1000,createjs.Ease.backOut);
            }
        }

    }

    p.onMouseDown = function(evt){
        createjs.Tween.removeTweens(this.obj);
        this.isPressed = true;
        this.real_y = this.obj.y;
        this.start_y = evt.stageY;


    }

    p.swipe = function(dir){
        //var cur_y = this.obj.y;
        var offset;
        var fudu = (this.obj.getBounds().height*this.obj.scaleX - this.objMask.h)/3;
        //console.log(this.offset_y);
        var during = 800;
        switch (dir){

            case "swipeup":

                offset = this.obj.y - Math.abs(this.offset_y);
                //console.log(offset,-(this.obj.getBounds().height*this.obj.scaleX-this.objMask.h))
                if(offset>-(this.obj.getBounds().height*this.obj.scaleX-this.objMask.h)){
                    //console.log("swipeup");
                    createjs.Tween.get(this.obj).to({y:offset}, during,createjs.Ease.circOut);
                }else{
                    createjs.Tween.get(this.obj).to({y:-(this.obj.getBounds().height*this.obj.scaleX-this.objMask.h)}, during,createjs.Ease.circOut);
                }

                break;
            case "swipedown":

                offset = this.obj.y + Math.abs(this.offset_y);
                //console.log(offset)
                if(offset<0){
                    //console.log("swipedown");
                    createjs.Tween.get(this.obj).to({y:offset}, during,createjs.Ease.circOut);
                }else{
                    createjs.Tween.get(this.obj).to({y:0}, during,createjs.Ease.circOut);
                }

                break;
        }
    }

    p.drawBg = function(){
        this.bg = new createjs.Shape();
        this.bg.graphics.beginFill("#000000").drawRect(0,0,this.objMask.w,this.objMask.h);
        this.bg.alpha = 0;

        this.addChild(this.bg);
    }

    p.showObj = function(){
        this.addChild(this.obj);
        if(this.fit.type == "exact_fit"){
            //console.log(this.obj.getBounds().width);
            //this.obj.scaleX = this.objMask / this.obj.getBounds().width;
            //this.obj.scaleY = this.obj.scaleX;
            var scale = this.objMask.w / this.obj.getBounds().width;
            //console.log(scale)
            this.obj.setTransform(0, 0, scale, scale);
            //console.log(this.obj.scaleX)
        }
    }

    p.drawMask = function(){
        this.s_mask = new createjs.Shape();
        this.s_mask.graphics.beginFill("#000000").drawRect(0,0,this.objMask.w,this.objMask.h);
        //this.addChild(this.s_mask);
        this.obj.mask = this.s_mask;
    }





    // add MyApp to myNameSpace
    window.eosi.website.Scroller = createjs.promote(Scroller, "Container");
//}());
}(window));





